class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> map;
        for(string n : words)
        {
            map[n]++;
        }
        priority_queue<pair<string, int>, vector<pair<string, int>>, cmp> pq;
        for(auto n : map)
        {
            pq.push(n);
        }
        vector<string> res;
        for(int i = 0; i < k; i++)
        {
            res.push_back(pq.top().first);
            pq.pop();
        }
        return res;
    }
    struct cmp {
        bool operator()(pair<string, int> a, pair<string, int> b) const
        {
            if(a.second < b.second) return true;
            if(a.second == b.second && a.first > b.first) return true;
            return false;
        }
    };
};
